package view.frames;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import model.Actions;
import utilities.EPatterns;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PatternsListFrame extends javax.swing.JFrame {

	/**
	 * CONSTANTES
	 */
	private static final String PATTERN_ADVISOR_TITLE = "Design Patterns";
	private static final String PATTERN_LIST_QUESTION = "Select a Design pattern from the list";
	
	/**
	 * 
	 */
	private EPatterns pattern = EPatterns.AbstractFactory;
	private Actions actions;
	private String chain = new String();
	
	/**
	 * GUI
	 */
	private static final long serialVersionUID = 1L;
	private JLabel patternAdvisorTitle;
	private JLabel patternListQuestion;
	private JButton noButton;
	private JButton yesButton;
	private JList<EPatterns> patternsList;
	private DefaultListModel<EPatterns> patternsListModel = new DefaultListModel<EPatterns>();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PatternsListFrame inst = new PatternsListFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public PatternsListFrame() {
		super();
		chain = new String();
		initGUI();
	}
	
	public PatternsListFrame(EPatterns pattern, String chain, Actions actions) {
		super();
		this.pattern = pattern;
		this.chain = chain;
		this.actions = actions;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setPreferredSize(new Dimension(650, 420));
			this.setResizable(false);
			GridBagLayout thisLayout = new GridBagLayout();
			this.setTitle("Pattern Advisor Wizard");
			thisLayout.rowWeights = new double[] {0.001, 0.01, 0.001};
			thisLayout.rowHeights = new int[] {1, 7, 1};
			thisLayout.columnWeights = new double[] {0.1};
			thisLayout.columnWidths = new int[] {7};
			this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/wizard.png")).getImage());
			getContentPane().setLayout(thisLayout);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			setLayout(new BorderLayout());
			JLabel background=new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/background_Main_1.png")));
			add(background);
			background.setLayout(thisLayout);

			{
				
				patternAdvisorTitle = new JLabel();
				patternAdvisorTitle.setText(PATTERN_ADVISOR_TITLE);
				patternAdvisorTitle.setFont(new Font("Curier New", 1, 22));
				background.add(patternAdvisorTitle, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
			}
			{
				
				patternListQuestion = new JLabel();
				patternListQuestion.setText(PATTERN_LIST_QUESTION);
				patternListQuestion.setFont(new Font("Curier New", 0, 15));
				background.add(patternListQuestion, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(20, 20, 0, 0), 0, 0));
			}
			{
				patternsList = new JList<EPatterns>();
				patternsList.setModel(patternsListModel);
				patternsList.setLayoutOrientation(JList.VERTICAL);
				patternsList.setVisibleRowCount(0);
				patternsList.setFont(new java.awt.Font("Calibri",0,12));

				JScrollPane scrollPane = new JScrollPane(patternsList,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				scrollPane.setPreferredSize(new java.awt.Dimension(600, 200));
				scrollPane.setMaximumSize(new java.awt.Dimension(600, 200));
				scrollPane.setMinimumSize(new java.awt.Dimension(600, 200));
				background.add(scrollPane, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 0), 0, 0));
				
				this.setUpListModel();

			}
			{
				yesButton = new JButton();
				yesButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/next.png")));
				yesButton.setText("Next ");
				yesButton.setToolTipText("Press Next to continue with the selected pattern");
				yesButton.setFont(new Font("Curier New",0,12));
				yesButton.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent evt) {
						yesButtonMousePressed(evt);
					}
				});
				background.add(yesButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 350, 3, 0), 25, 0));
			}
			{
				noButton = new JButton();
				noButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/cancel.png")));
				noButton.setText("Cancel ");
				noButton.setToolTipText("Press Cancel to exit the wizard");
				noButton.setFont(new java.awt.Font("Curier New",0,12));
				noButton.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent evt) {
						noButtonMousePressed(evt);
					}
				});
				background.add(noButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 465, 3, 0), 10, 0));
			}
					
			pack();
			setSize(650, 420);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	/**
	 * Cargo la lista con todos los patrones
	 */
	private void setUpListModel() {
		
		patternsListModel.addElement(EPatterns.AbstractFactory);	
		patternsListModel.addElement(EPatterns.Adapter);	
		patternsListModel.addElement(EPatterns.Bridge);	
		patternsListModel.addElement(EPatterns.Builder);	
		patternsListModel.addElement(EPatterns.ChainOfResponsability);	
		patternsListModel.addElement(EPatterns.Command);	
		patternsListModel.addElement(EPatterns.Composite);	
		patternsListModel.addElement(EPatterns.Decorator);	
		patternsListModel.addElement(EPatterns.FactoryMethod);	
		patternsListModel.addElement(EPatterns.Flyweight);	
		patternsListModel.addElement(EPatterns.Interpreter);	
		patternsListModel.addElement(EPatterns.Iterator);	
		patternsListModel.addElement(EPatterns.Mediator);	
		patternsListModel.addElement(EPatterns.Memento);	
		patternsListModel.addElement(EPatterns.Observer);	
		patternsListModel.addElement(EPatterns.Prototype);	
		patternsListModel.addElement(EPatterns.Proxy);	
		patternsListModel.addElement(EPatterns.State);	
		patternsListModel.addElement(EPatterns.Strategy);	
//		patternsListModel.addElement(EPatterns.TemplateMethod);	
		patternsListModel.addElement(EPatterns.Visitor);	
		
	}

	protected void noButtonMousePressed(MouseEvent evt) {
		this.dispose();
	}

	protected void yesButtonMousePressed(MouseEvent evt) {
		
		this.dispose();
		
		if (!patternsList.isSelectionEmpty()) {
			this.pattern = patternsList.getSelectedValue();
		}
		
		JFrame patternRecommendator;
	
		patternRecommendator = new PatternRecommendatorFrame(pattern,chain,actions);
	
		patternRecommendator.setLocationRelativeTo(null);
		patternRecommendator.setVisible(true);
		
	}
}
